function getProductType() {
    try {
        var url = GET_CATEGORIES;
        var xhr = createXHR();
        xhr.open("GET", url, true);
        xhr.setRequestHeader("Content-type", "application/json;charset=utf-8");
        xhr.send();
        console.log("栏目[url]:" + url);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    var str = xhr.responseText;
                    if (str != '' && str != null && str != undefined) {
                        var obj = eval('(' + str + ')');
                        if (obj != null && obj.resultCode == 1) {
                            if (obj.data.length > 0) {
                                navJson = obj.data;
                                showProductType();
                            }
                        } else {
                            showMessage(errorMsg.contentError);
                        }
                    } else {
                        showMessage(errorMsg.contentError);
                    }
                } else {
                    showMessage(errorMsg.network);
                }
            }
        }
    } catch (e) {
        showMessage(errorMsg.network);
    }

}


function getProductList(id) {
    try {
        var url = 'data/contents.json';
        /* if (type == 2) {*/
        if (id != null) {
            url = GET_FINANCIAL_PRODUCTS_CATEGORIES + id;
            /*'data/data3.json'*/
        } else {
            showMessage(errorMsg.parameter);
            return;
        }
        /*  }*/
        var xhr = createXHR();
        xhr.open("GET", url, true);
        xhr.setRequestHeader("Content-type", "application/json;charset=utf-8");
        xhr.send();
        console.log("列表[url]:" + url);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    var str = xhr.responseText;
                    if (str != '' && str != null && str != undefined) {
                        var obj = eval('(' + str + ')');
                        if (obj != null && obj.resultCode == 1) {
                            if (obj.data.length > 0) {
                                productJson = obj.data;
                                currData = productJson.sort(descend("id"));
                                showProductList();
                            }
                        } else {
                            showMessage(errorMsg.contentError);
                        }
                    } else {
                        showMessage(errorMsg.contentError);
                    }
                } else {
                    showMessage(errorMsg.network);
                }
            }
        }
    } catch (e) {
        showMessage(errorMsg.network);
    }

}

//获取保险详情
function getInsuranceObj(id) {
    try {
        if (id != null) {
            var url =GET_PRODUCTS_INFO + id;
        } else {
            return;
        }
        var xhr = createXHR();
        xhr.open("GET", url, true);
        xhr.setRequestHeader("Content-type", "application/json;charset=utf-8");
        xhr.send();
        console.log("url:" + url);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    var str = xhr.responseText;
                    if (str != '' && str != null && str != undefined) {
                        var obj = eval('(' + str + ')');
                        if (obj != null && obj.resultCode == 1) {
                            if (obj.data != null) {
                                insuranceObj = obj.data;
                                showInsuranceObj();
                            }
                        } else {
                            showMessage(errorMsg.contentError);
                        }
                    } else {
                        showMessage(errorMsg.contentError);
                    }
                } else {
                    showMessage(errorMsg.network);
                }
            }
        }
    } catch (e) {
        showMessage(errorMsg.network);
    }
}


// 用户姓名 手机号 身份证号 费用 产品编号 开始时间  结束时间
function payment(username, phone, idCard, price, proId, start, end, des, type,fenshu) {
    Util.showLoading();
    var data = {
        "proid": proId,
        "name": username,
        "phone": phone,
        "idcard": idCard,
        "starttime": start,
        "endtime": end,
        "fenshu":fenshu
    };
    var inWitch = 'alipay';
    if (type == 2) {
        inWitch = 'qrcodepay';
    }
    var url = SERVER_IP + "v1/" + inWitch + "/pay";
    var xhr = createXHR();
    xhr.open("POST", url, true);
    xhr.setRequestHeader("Content-type", "application/json;charset=utf-8");
    xhr.send(JSON.stringify(data));
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                var str = xhr.responseText;
                if (str != '' && str != null && str != undefined && str != 0) {
                    var src = SERVER_IP + "v1/spqrcodepay/qrcode/" + str;
                    document.getElementById("smCode").innerHTML = '<img style="height: 400px;width: 400px;" src="' + src + '"/>';
                    /*var time = 1800000;
                     if(type==2){
                     time = 190000
                     }*/
                    //timerOver = setTimeout("isOvertime()",time);
                    timerPay = setInterval(function () {
                        clock(str, type);
                    }, 3000);
                } else {
                    getCodeError();
                    showMessage(errorMsg.qrcInfo);
                }
                Util.hideLoading();
            } else {
                getCodeError();
                Util.hideLoading();
                showMessage(errorMsg.network);
            }
        }
        Util.hideLoading();
    }
}


function isOvertime() {
    clearInterval(timerOver);
    abortAll();
    getCodeError("二维码已过期，请退出重试！");
}


function clock(orderId, type) {
    var url = SERVER_IP + "v1/wxpay/research/" + orderId;
    if (type == 1) {
        url = SERVER_IP + "v1/alipay/view/" + orderId;
    }
    var xhr = createXHR();
    xhr.open("GET", url, true);
    xhr.setRequestHeader("Content-type", "application/json;charset=utf-8");
    xhr.send();
    ajaxArray.push(xhr);
    console.log('[获取订单状态：]' + url);
    xhr.onreadystatechange = function () {
        if (isClosed == true) {
            abortAll();
            return;
        }
        if (xhr.readyState === 4 && xhr.status === 200) {
            var str = xhr.responseText;
            var obj = eval('(' + str + ')');
            if (type == 1) {
                if (obj.alipay_trade_query_response.code == 10000 && obj.alipay_trade_query_response.trade_status == 'TRADE_SUCCESS') {
                    //1.未支付 2.已支付
                    clearInterval(timerOver);
                    abortAll();
                    successPage();
                } else if (obj.alipay_trade_query_response.code == 'OVERTIME') {//二维码过期
                    isOvertime();
                } else if (obj.alipay_trade_query_response.code == 'NOBUY') {//已购买
                    clearInterval(timerOver);
                    abortAll();
                    errorPage("请勿重复购买！详情致电96655。");
                }
            } else if (type == 2) {
                if (obj.result_code == 'SUCCESS' && obj.trade_state == 'SUCCESS') {
                    //1.未支付 2.已支付
                    clearInterval(timerOver);
                    abortAll();
                    successPage();
                } else if (obj.result_code == 'OVERTIME') {//二维码过期
                    isOvertime();
                } else if (obj.result_code == 'NOBUY') {//已购买
                    clearInterval(timerOver);
                    abortAll();
                    errorPage("请勿重复购买！详情致电96655。");
                }
            }

        }
    }
}

function abortAll() {
    if (ajaxArray.length > 0) {
        clearInterval(timerPay);
        for (var i = 0; i < ajaxArray.length; i++) {
            if (i < ajaxArray.length && ajaxArray[i] != null) {
                ajaxArray[i].abort();
            }
        }
    }
    isClosed = false;
    ajaxArray = [];
}

function getCodeError(msg) {
    var info = '二维码加载失败，请重试。';
    if (msg != '' && msg != undefined && msg != null) {
        info = msg;
    }
    document.getElementById("smCode").innerHTML = '<img src="img/a_06.png" class="smErrorImg"> ' +
        '<span class="smErrorInfo">' + info + '</span>';
}


//获取用户信息
function getUserInfo() {
    var card = Util.getCardSeruakNumber();
    var url = GET_USER_INFO + card;
    console.log('[获取用户信息]：' + url);
    var ret = getHttpRequest(url);
    if (ret != null && ret != '') {
        try {
            var resultObj = eval("(" + ret + ")");
            if (resultObj.resultCode == 1) {
                var obj = resultObj.data;
                if (obj.data != null) {//使用
                    if (obj.data.custname != null && obj.data.custname != '' && obj.data.phone != null
                        && obj.data.phone != '' && obj.data.idcardid != null && obj.data.idcardid != '') {
                        return obj.data;
                    }else {
                        showMessage("用户信息异常，请退出重试！如有疑问，请致电96655！");
                    }
                }
            } else {
                showMessage("用户信息异常，请退出重试！如有疑问，请致电96655！");
            }
        } catch (error) {
            showMessage("用户信息异常，请退出重试！如有疑问，请致电96655！");
        }
    }
}
function getHttpRequest(_url) {
    var request = new window.XMLHttpRequest();
    var ret = null;
    try {
        request.open("GET", _url, false);
        request.send(null);
        ret = request.responseText;
        if (ret != "") {
            ret = ret.replace(/(\r\n)|(\r)|(\n)/gi, "");
            //ret = ret.replace(/(\\)/gi, "\\\\");
            //ret = ret.replace(/(\)/gi, "\\\");
        }
    } catch (error) {
        showMessage("网络异常，请退出重试！");
    }
    return ret;
}